#!/bin/bash

#
# $Id: openars_api.sh 150 2011-04-06 17:00:59Z mamartynov@gmail.com $
#

. /home/admin/.bash_profile

SCRIPT=$0
#while [ -h "$SCRIPT" ]; do SCRIPT=`ls -l "$SCRIPT" | awk '{print $NF}'`; done
SCRIPT_BASENAME=`basename $SCRIPT`
SCRIPT_DIRNAME=`dirname $SCRIPT`
echo $SCRIPT_DIRNAME | grep ^/ &>/dev/null
[ $? -ne 0 ] && SCRIPT_DIRNAME=`echo $SCRIPT_DIRNAME | sed s!^!$PWD/!`

if [ -z "$OPENARS_HOME" ]; then
    echo "please, define and export OPENARS_HOME in your .bash_profile. exit 1." >&2
    exit 1
fi

if [ ! -d "$OPENARS_HOME" ]; then
    echo "not find OPENARS_HOME directory: $OPENARS_HOME. exit 1." >&2
    exit 1
fi


function usage() {
cat >&2 <<EOF
Usage: $SCRIPT_BASENAME FUNCTION [OPTIONS]
Known FUNCTION are:
    setenv
    getenv
    get_hosts
    check_key
    check_dns
    zconfigs.make
    zconfigs.check_crc
    zconfigs.install.hostlist
    zconfigs.install.check
    zconfigs.install
    zscripts.check_crc
    zscripts.list
    zscripts.hostlist
    zscripts.install.hostlist
    zscripts.install.check
    zscripts.install
    zagent.install
    get_profile_info
    get_df_info
    get_users_info
    user_add
    user_del
in detail see http://code.google.com/p/openars/wiki/api_functions
EOF
}

function setenv() {
    echo function \"setenv\" is not supported >&2
    return 1
}

function getenv() {
    echo function \"getenv\" is not supported >&2
    return 1
}
function get_hosts() {
    echo function \"get_hosts\" is not supported >&2
    return 1
}

function check_key() {
    runfile="$OPENARS_HOME/scripts/check_key.sh"
    if [ ! -x "$runfile" ]; then
        echo "not find $runfile. exit 1." >&2
        return 1
    fi
    $runfile $@
    return $?
}

function check_dns() {
    runfile="$OPENARS_HOME/scripts/check_dns.sh"
    if [ ! -x "$runfile" ]; then
        echo "not find $runfile. exit 1." >&2
        return 1
    fi
    $runfile $@
    return $?
}

function zconfigs.make() {
    echo function \"zconfigs.make\" is not supported >&2
    return 1
}

function zconfigs.check_crc() {
    echo function \"zconfigs.check_crc\" is not supported >&2
    return 1
}

function zconfigs.install.hostlist() {
    echo function \"zconfigs.install.hostlist\" is not supported >&2
    return 1
}

function zconfigs.install.check() {
    echo function \"zconfigs.install.check\" is not supported >&2
    return 1
}

function zconfigs.install() {
    echo function \"zconfigs.install\" is not supported >&2
    return 1
}

function zscripts.check_crc() {
    echo function \"zscripts.check_crc\" is not supported >&2
    return 1
}

function zscripts.list() {
    echo function \"zscripts.lis\" is not supported >&2
    return 1
}

function zscripts.hostlist() {
    echo function \"zscripts.hostlist\" is not supported >&2
    return 1
}

function zscripts.install.hostlist() {
    echo function \"zscripts.install.hostlist\" is not supported >&2
    return 1
}

function zscripts.install.check() {
    echo function \"zscripts.install.check\" is not supported >&2
    return 1
}

function zscripts.install() {
    echo function \"zscripts.install\" is not supported >&2
    return 1
}

function zagent.install() {
    runfile="$OPENARS_HOME/scripts/zabbix_manager/install_agent.sh"
    if [ ! -x "$runfile" ]; then
        echo "not find $runfile. exit 1." >&2
        return 1
    fi
    $runfile $@
    return $?
}

function get_profile_info() {
    runfile="$OPENARS_HOME/scripts/get_profile_info.sh"
    if [ ! -x "$runfile" ]; then
        echo "not find $runfile. exit 1." >&2
        return 1
    fi
    $runfile $@
    return $?
}

function get_df_info() {
    runfile="$OPENARS_HOME/scripts/get_df_info.sh"
    if [ ! -x "$runfile" ]; then
        echo "not find $runfile. exit 1." >&2
        return 1
    fi
    $runfile $@
    return $?
}

function get_users_info() {
    runfile="$OPENARS_HOME/scripts/get_users_info.sh"
    if [ ! -x "$runfile" ]; then
        echo "not find $runfile. exit 1." >&2
        return 1
    fi
    $runfile $@
    return $?
}

function get_sshkeys_info() {
    runfile="$OPENARS_HOME/scripts/get_sshkeys_info.sh"
    if [ ! -x "$runfile" ]; then
        echo "not find $runfile. exit 1." >&2
        return 1
    fi
    $runfile $@
    return $?
}

function user_add() {
    runfile="$OPENARS_HOME/scripts/user_add.sh"
    if [ ! -x "$runfile" ]; then
        echo "not find $runfile. exit 1." >&2
        return 1
    fi
    $runfile $@
    return $?
}

function user_del() {
    runfile="$OPENARS_HOME/scripts/user_del.sh"
    if [ ! -x "$runfile" ]; then
        echo "not find $runfile. exit 1." >&2
        return 1
    fi
    $runfile $@
    return $?
}

[ $# -eq 0 ] && { usage; exit 1; }

while [ $# -gt 0 ]
do
        case "$1" in
        setenv)
                shift
                setenv $@
                exit $?
                ;;
        getenv)
                shift
                getenv $@
                exit $?
                ;;
        get_hosts)
                shift
                get_hosts $@
                exit $?
                ;;
        check_key)
                shift
                check_key $@
                exit $?
                ;;
        check_dns)
                shift
                check_dns $@
                exit $?
                ;;
        zconfigs.make)
                shift
                zconfigs.make $@
                exit $?
                ;;
        zconfigs.check_crc)
                shift
                zconfig.check_crc $@
                exit $?
                ;;
        zconfigs.install.hostlist)
                shift
                zconfigs.install.hostlist $@
                exit $?
                ;;
        zconfigs.install.check)
                shift
                zconfigs.install.check $@
                exit $?
                ;;
        zconfigs.install)
                shift
                zconfigs.install $@
                exit $?
                ;;
        zscripts.check_crc)
                shift
                zscripts.check_crc $@
                exit $?
                ;;
        zscripts.list)
                shift
                zscripts.list $@
                exit $?
                ;;
        zscripts.hostlist)
                shift
                zscripts.hostlist $@
                exit $?
                ;;
        zscripts.install.hostlist)
                shift
                zscripts.install.hostlist $@
                exit $?
                ;;
        zscripts.install.check)
                shift
                zscripts.install.check $@
                exit $?
                ;; 
        zscripts.install)
                shift
                zscripts.install $@
                exit $?
                ;;
        zagent.install)
                shift
                zagent.install $@
                exit $?
                ;;
        get_profile_info)
                shift
                get_profile_info $@
                exit $?
                ;;
        get_df_info)
                shift
                get_df_info $@
                exit $?
                ;;
        get_users_info)
                shift
                get_users_info $@
                exit $?
                ;;
        get_sshkeys_info)
                shift
                get_sshkeys_info $@
                exit $?
                ;;
        user_add)
                shift
                user_add $@
                exit $?
                ;;
        user_del)
                shift
                user_del $@
                exit $?
                ;;
        *)
                echo unknown function \"$1\" >&2
                usage
                exit 1
                ;;
        esac
done

